import React, { Component } from 'react';


import md5 from '../util/Md5Util'
import AxiosUtil from '../util/AxiosUtil'
import JwtUtil from '../util/JwtUtil'



import './less/login.less';





import { Form, Icon, Input, Button, Checkbox, message } from 'antd';

const FormItem = Form.Item;

class NormalLoginForm extends React.Component {


  dealLoginOk = (resp) => {
    let that = this;
    let { mt, jwt, data } = resp.data;

    if (mt && mt.suc && jwt) {
      JwtUtil.decode(jwt,
        function (decoded) {
          

          console.log("jwt:", decoded);

          setTimeout(function () { //延迟进入
            that.props.history.push({ pathname: '/app', state: resp.data });
          }, 200);
        },
        function (err) { message.error("jwt error:" + err, 5); }
      );

    } else {
      message.error("something error", 5);
    }
  }

  handleSubmit = (e) => {
    e.preventDefault();
    this.props.form.validateFields((err, values) => {
      if (!err) {
        console.log('Received values of form: ', values);
        console.log('Received : ', values.password);
        console.log('Received : ', md5(values.password));


        values.password = md5(values.password);
        let that = this;

        AxiosUtil.post("/user/loginuser", values, this.dealLoginOk,
          function (error) {
            if (error.response) {
              //请求已经发出，但是服务器响应返回的状态吗不在2xx的范围内
              console.log(error.response.data);
              console.log(error.response.status);
              console.log(error.response.header);

              message.error(error.response.data.mt.msg, 5);
            } else {
              //一些错误是在设置请求的时候触发
              console.log('Error', error.message);
            }
            console.log(error.config);
          }
        );

      }
    });
  }

  render() {
    const { getFieldDecorator } = this.props.form;
    return (

      <Form onSubmit={this.handleSubmit} className="login-form">
        <FormItem>
          {getFieldDecorator('username', {
            rules: [{ required: true, message: 'Please input your username!' }],
          })(
            <Input prefix={<Icon type="user" style={{ color: 'rgba(0,0,0,.25)' }} />} placeholder="Username" />
          )}
        </FormItem>
        <FormItem>
          {getFieldDecorator('password', {
            rules: [{ required: true, message: 'Please input your Password!' }],
          })(
            <Input prefix={<Icon type="lock" style={{ color: 'rgba(0,0,0,.25)' }} />} type="password" placeholder="Password" />
          )}
        </FormItem>
        <FormItem>
          {getFieldDecorator('remember', {
            valuePropName: 'checked',
            initialValue: true,
          })(
            <Checkbox>Remember me</Checkbox>
          )}
          <a className="login-form-forgot" href="">Forgot password</a>
          <Button type="primary" htmlType="submit" className="login-form-button">
            Log in
          </Button>
          Or <a href="">register now!</a>
        </FormItem>
      </Form>

    );
  }
}

const WrappedNormalLoginForm = Form.create()(NormalLoginForm);


export default WrappedNormalLoginForm;